package service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

/**
 * @Description: 管理客户端线程类
 * @CreateTime: 2024-02-26  14:57
 * @Author: JAMILA
 * @Version: 1.0
 */

public class ManageClientThreads {
    private static HashMap<String, ServerConnectClientThread> hm = new HashMap<>();

    //添加线程
    public static void addServerConnectClientThread(String userId, ServerConnectClientThread serverConnectClientThread) {
        hm.put(userId, serverConnectClientThread);
    }

    //根据用户号获取线程
    public static ServerConnectClientThread getServerConnectClientThread(String userId) {
        return hm.get(userId);
    }

    //返回在线用户列表
    public static String getOnlineUsers() {
        String res = "";
        Set<String> keySet = hm.keySet();
        for (String s :
                keySet) {
            res += s + " ";
        }
        return res;
    }

    //删除线程
    public static void removeServerConnectClientThread(String userId){
        hm.remove(userId);
    }

    //获取所有在线用户线程
    public static List<ServerConnectClientThread> getAllServerConnectClientThread(String senderId){
        List<ServerConnectClientThread> res=new ArrayList<>();
        Set<String> keySet = hm.keySet();
        for (String key : keySet) {
            if (!senderId.equals(key)){
                res.add(hm.get(key));
            }
        }
        return res;
    }

    //判断用户是否在线
    public static boolean isOnline(String userId){
        return hm.containsKey(userId);
    }
}
